<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;


use App\CustHub\Model\ChSeat;
use Hyperf\DbConnection\Db;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;


class ChGetSeatMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChSeat::class;
    }


    public function getSeat($params): array
    {
        $filters = [
            'use_status' => '=',
            'status' => '=',
            'seat_type' => '=',
            'corp_name' => 'like'
        ];
        $query = $this->model::query()
            ->leftJoin('ch_corp', 'ch_corp.corp_id', '=', 'ch_seat.corp_id')
            ->select([
                'ch_seat.id',
                'ch_seat.code',
                'ch_seat.use_status',
                'ch_seat.expire_time',
                DB::raw("FROM_UNIXTIME(ch_seat.expire_time, '%Y-%m-%d %H:%i:%s') as expire_time_formatted"), // 转换时间戳
                'ch_seat.status',
                'ch_seat.is_auto_renewal',
                'ch_seat.seat_type',
                'ch_corp.corp_name as corp_name',
                'ch_seat.created_at'
            ])
            ->distinct()
            ->orderBy('ch_seat.id', 'desc');
        $query = applyFilters($query, $params, $filters);
        $paginate = $query->paginate((int)$params['pageSize']?? MineModel::PAGE_SIZE);


        return $this->setPaginate($paginate);
    }
}
